package com.prs.crm.service.fs.impl;

import java.io.File;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.fs.OutlayReturnDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.fs.Detail;
import com.prs.crm.domain.fs.OutlayReturn;
import com.prs.crm.service.fs.DetailService;
import com.prs.crm.service.fs.OutlayReturnService;
import com.prs.crm.service.fs.OutlayService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class OutlayReturnServiceImpl extends BaseServiceImpl<OutlayReturn,OutlayReturnDao> implements OutlayReturnService{
	private DetailService detailService;
	private OutlayService outlayService;
	
	@Override
	public void submitOutlayReturn(OutlayReturn outlayReturn, File file,
			String fileName) {
		// TODO Auto-generated method stub
		Attachment attach = FileUtil.getInstance().createAttach(file, fileName);
		if(attach != null) {
			outlayReturn.setAttach(attach);			
		}
		this.getDao().save(outlayReturn);
		outlayReturn.getOutlay().setReturnFlag(Constrains.FS_FLAG_RETURN);
		this.getOutlayService().save(outlayReturn.getOutlay());
		
		Double sum = this.getDetailService().getBalanceByAccount(outlayReturn.getBankAccount());
		Detail detial = new Detail();
		detial.setOutlayReturn(outlayReturn);
		detial.setIncome(outlayReturn.getMoney());
		detial.setPay(0.0);
		detial.setBalance(sum + outlayReturn.getMoney());
		detial.setBankAccount(outlayReturn.getBankAccount());
		detial.setItem(Constrains.FS_ITEMS_OUTLAYRETURN);		
		this.getDetailService().save(detial);
	}

	public void setDetailService(DetailService detailService) {
		this.detailService = detailService;
	}

	public DetailService getDetailService() {
		return detailService;
	}

	public void setOutlayService(OutlayService outlayService) {
		this.outlayService = outlayService;
	}

	public OutlayService getOutlayService() {
		return outlayService;
	}

}
